

# Get a dictionary that maps each tag to the set of bones with that tag.
# Obj is an armature object. 
def get_tags_dict(obj):
    tags = dict()
    for bone in obj.pose.bones:
        for t in get_bone_tags(obj, bone.name):        
            if tags.get(t) is None: # tag not yet in dictionary as key 
                tags[t] = set()
            tags[t].add(bone.name)   
    return tags


# Get all tags of a certain bone.
# Obj is an armature object.
def get_bone_tags(obj, bone_name):
    bone = obj.pose.bones[bone_name]
    
    bone_tags = bone.get("@tags") # the tags are in a custom property
    
    tags = set()
    if bone_tags is not None:
        for tag in bone_tags.split(","):
            t = tag.strip() # remove whitespace
            tags.add(t)
            
    return tags
    